/**
 *
 */
package org.yabam.action;

import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

import org.yabam.model.Category;

/**
 * @author brlau69n
 *
 */
public class EditCategoriesAction extends YabamActionBean {

	@ValidateNestedProperties( { @Validate(field = "name", required = true, on = "add") })
	private Category category;

	@ValidateNestedProperties( { @Validate(field = "id", required = true, on = "delete") })
	private List<Category> selectedCategories;


	public Resolution addOrEdit() {
		Category parent = category.getParent();
		if (parent != null) {
			category.setParent(Category.get(parent.getId()));
		}
		category.saveOrUpdate();
		return load();

	}

	@DefaultHandler
	public Resolution load() {
		return new RedirectResolution("/editCategories.jsp");
	}

	public Resolution delete() {
		for (Category cat : selectedCategories) {
			if (cat != null)
				cat.delete();
		}
		return load();
	}

	/**
	 * @return the category
	 */
	public Category getCategory() {
		return category;
	}

	/**
	 * @param category
	 *            the category to set
	 */
	public void setCategory(Category category) {
		this.category = category;
	}

	/**
	 * @return the selectedCategories
	 */
	public List<Category> getSelectedCategories() {
		return selectedCategories;
	}

	/**
	 * @param selectedCategories
	 *            the selectedCategories to set
	 */
	public void setSelectedCategories(List<Category> selectedCategories) {
		this.selectedCategories = selectedCategories;
	}
}
